package com.atguigu.reggie.controller;

import com.atguigu.reggie.common.BaseContext;
import com.atguigu.reggie.common.R;
import com.atguigu.reggie.entity.ShoppingCart;
import com.atguigu.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Luo Sheng
 * @school YiBin university
 * @create 2022-10-25 10:49
 */

/**
 * 购物车后端逻辑
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    /**
     * 添加菜品到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);

        // 设置用户id，指定是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        // 查询当前添加的菜品或者是套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();

        // 创建查询构造器
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());

        if (dishId != null){
            // 添加到购物车的是菜品，动态添加条件
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else {
            // 添加到购物车的是套餐，动态添加条件
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }

        // 查询当前菜品或者套餐是否在购物车中
        // SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);

        // 进行判断
        if (shoppingCartServiceOne != null){
            // 如果已经存在，在原来的数量基础上加上一
            Integer number = shoppingCartServiceOne.getNumber();
            shoppingCartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(shoppingCartServiceOne);

        }else {
            //  如果不存在，则添加到购物车
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartServiceOne = shoppingCart;
        }

        // 返回更新后的shoppingCart
        return R.success(shoppingCartServiceOne);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车");

        // 常见查询构造器，并添加查询条件
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartLambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        // 执行查询
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);


        return R.success(list);
    }

    /**
     * 清空当前用户的购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        shoppingCartService.clean();
        // 返回
        return R.success("购物车清空成功");
    }

    /**
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("shoppingCart：{}",shoppingCart);

        // 创建查询构造器
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId()) ;

        // 设置用户id，指定是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        // 查询当前减少的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();

        // 判断当前要减少的是菜品还是套餐
        if (dishId != null){
            // 当前要减少的是菜品，动态修改
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else {
            // 当前要减少的是套餐，动态修改
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }

        // 查询当前菜品货套餐的分数是否为1
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        Integer number = shoppingCartServiceOne.getNumber();
        // 进行判断
        if (number > 1){
            // 如果当前菜品或套餐的数量大于1，修改数量
            number = number - 1;
            shoppingCartServiceOne.setNumber(number);

            // 执行修改
            shoppingCartService.updateById(shoppingCartServiceOne);

            return R.success(shoppingCartServiceOne);

        }else {
            // 如果当前菜品或套餐的数量等于1，直接删除
            shoppingCartService.removeById(shoppingCartServiceOne);

            // 修改shoppingCartService数据并返回前端
            shoppingCartServiceOne.setNumber(0);

            return R.success(shoppingCartServiceOne);
        }

    }
}
